<?php include("/includes/header.php") ?>
<?php include("/includes/sidebar.php") ?>
<?php $idfo = base64_decode($_GET[trim(base64_encode("id"), '=')]); ?>
<?php
	$gender = array('m' => 'Male', 'f' => 'Female');
	$sql = "SELECT * FROM front_office WHERE id = '".$idfo."'";
	$query = mysql_query($sql);
	$data = mysql_fetch_array($query);
	$data = validateData($data);
	extract($data);

	// SELECTING TRAVEL AGEN
	$sql_select_agent = "SELECT * FROM agent WHERE id = '".$idagent."'";
	$query_select_agent = mysql_query($sql_select_agent);
	$data_agent = mysql_fetch_array($query_select_agent);

	// SELECTING SERVICE CHARGE
	$sql_select_service_charge = "SELECT * FROM service_charge WHERE idfront_office = '".$idfo."'";
	$query_select_service_charge = mysql_query($sql_select_service_charge);

	// SELECTING TAX SERVICE
	$sql_select_tax_service = "SELECT * FROM setting";
	$query_select_tax_service = mysql_query($sql_select_tax_service);
	$data_tax_service = mysql_fetch_array($query_select_tax_service);
	$tax_service = $data_tax_service["tax"]+$data_tax_service["service"];

	// SELECTING ROOM INFORMATION
	$data_room = getRoom($idroom);

	// CALCULATE THE DATE BETWEEN CHECKOUT AND CHECKIN
	$diff = abs(strtotime($checkout) - strtotime($checkin));
	$days = floor($diff / (60*60*24));
	// $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
	// $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

	// printf("%d years, %d months, %d days\n", $years, $months, $days);
	$grand_total = $data_room["harga"] * $days * $banyak_orang;
?>

<style type="text/css">
	tbody tr:not(:first-child) td {
		border-top: none !important;
		/*border-bottom: none !important;*/
	}
	tbody tr:not(:last-child) td {
		border-bottom: 1px dotted #ccc !important;
		/*border-bottom: none !important;*/
	}
</style>

<!-- <div class="header">
	<span>
		<a href="room.php">
			<i class="fa fa-building-o"></i> Data Kamar
		</a>
	</span>
	<span>
		<a href="room-form.php?act=new">
			<i class="fa fa-plus"></i> Tambah Kamar
		</a>
	</span>
</div> -->

<div class="content row-fluid">
	<div class="span12 table-placer">
		<div class="alert alert-info"><h4>Service Charge - <?php echo $data_room["nama"]; ?></h4></div>
		<div class="row-fluid index-pos-placer content">
			<?php
				if ($is_paid == 1) {
			?>
			<div class="alert alert-info">
			  <p>
			  	This person has already been checkout.
			  	<br>
			  	<a href="fo-index.php" class="btn esc">Exit (ESC)</a>
			  </p>
			</div>
			<?php
				} else {
			?>
			<form id="form_invoice" action="action/act_fo.php" class="" method="POST">
				<div class="row-fluid">
					<fieldset>
						<table class="table table-bordered">
							<thead>
								<tr>
									<th class="al_center" width="50%">Description</th>
									<th class="al_center" width="10%">Price</th>
									<th class="al_center" width="10%">Guests</th>
									<th class="al_center" width="10%">Quantity</th>
									<th class="al_center" width="20%">Amount</th>
								</tr>
							</thead>
							<tbody id="invoice">
								<?php
									if (mysql_num_rows($query) > 0) {
										while ($data_service_charge = mysql_fetch_array($query_select_service_charge)) {
											$grand_total += ($data_service_charge["jumlah"]*$data_service_charge["harga"]);
								?>
								<tr>
									<td class="al_left"><?php echo $data_service_charge["nama"]; ?></td>
									<td class="al_right currency" valign="middle"><?php echo $data_service_charge["harga"]; ?></td>
									<td class="al_center" valign="middle">-</td>
									<td class="al_center" valign="middle"><?php echo $data_service_charge["jumlah"]; ?></td>
									<td class="al_right currency amount_inv" valign="middle"><?php echo ($data_service_charge["jumlah"]*$data_service_charge["harga"]); ?></td>
								</tr>
								<?php
										}
									}
								?>
							</tbody>
							<tfoot>
								<tr>
									<td class="al_right" colspan="5" valign="middle">
										<span class="pull-left">
											<button id="add_new_sc" class="btn f3"><i class="fa fa-plus"></i> Add Service Charge (F3)</button>
											<button id="delete_last_sc" class="btn f4"><i class="fa fa-trash-o"></i> Remove Last Line (F4)</button>
										</span>
									</td>
								</tr>
							</tfoot>
						</table>
					</fieldset>
				</div>
				<fieldset>
					<div class="form-actions">
						<button type="submit" class="btn btn-primary f2">Submit (F2)</button>
						<a id="exit_fo" href="fo-index.php" class="btn esc">Cancel (ESC)</a>
					</div>
				</fieldset>
			</form>
			<?php
				}
			?>

		</div>
	</div>
</div>

<div class="footer">
</div>

<?php include("/includes/footer.php") ?>

<script type="text/javascript">

	$(document).ready(function() {
		var elPop;

		$('#add_new_sc').click(function(event) {
			/* Act on the event */
			event.preventDefault();
			$('#invoice').append("<tr class='added_row'>"+
				"<td class='al_left'><input type='text' class='input-xlarge name_sc'></td>"+
				"<td class='al_center'><input type='text' class='input-small currency_sc al_right price_sc'></td>"+
				"<td class='al_center'>-</td>"+
				"<td class='al_center'><input type='text' class='input-small qty_sc al_center'></td>"+
				"<td class='al_right amount_sc amount_inv'>0</td>"+
			"</tr>");

			$('.currency_sc').autoNumeric('init', {
				aSign: 'IDR ',
				aPad: false
			});

			$('.amount_sc').autoNumeric('init', {
				aSign: 'IDR ',
				aPad: false
			});

			$(".qty_sc").spinedit({
		    minimum: 1,
		    step: 1,
		    value: 0
			});

			$('#invoice .added_row:last .name_sc').focus();
		});

		$('#invoice').on('keyup', '.price_sc', function(event) {
			/* Act on the event */
			var that = $(this);
			var parent = that.closest('tr');
			var amount = parent.find('.qty_sc').val() * that.autoNumeric('get');
			parent.find('.amount_sc').autoNumeric('set', (amount));
			// getGrandTotal();
		});

		$('#invoice').on('valueChanged', '.qty_sc', function(event) {
			// event.preventDefault();
			/* Act on the event */
			var that = $(this);
			var parent = that.closest('tr');
			var amount = parent.find('.price_sc').autoNumeric('get') * that.val();
			parent.find('.amount_sc').autoNumeric('set', (amount));
			// getGrandTotal();
		});

		$('#spinEdit').on("valueChanged", function (e) {
		});

		$('#delete_last_sc').click(function(event) {
			/* Act on the event */
			event.preventDefault();
			$('#invoice .added_row:last').remove();
			$('#invoice .added_row:last .name_sc').focus();
			// getGrandTotal();
		});

		$('#form_invoice').submit(function(event) {
			/* Act on the event */
			event.preventDefault();
			var serviceChargeArr = [];
			$('.added_row').each(function(index, el) {
				var serviceChargeRowArr = {};
				serviceChargeRowArr.nama = $(el).find('.name_sc').val();
				serviceChargeRowArr.jumlah = $(el).find('.qty_sc').val();
				serviceChargeRowArr.harga = $(el).find('.price_sc').autoNumeric('get');
				serviceChargeArr.push(serviceChargeRowArr);
			});
			var dataCheckout = {
				act: "service_charge",
				id: '<?php echo $idfo; ?>',
				sc: serviceChargeArr
			};
			$.ajax({
				url: 'action/act_fo.php',
				type: 'POST',
				dataType: 'json',
				data: dataCheckout,
				cache: false,
			})
			.done(function(data) {
				// console.log(data);
				if (data.status == '1') {
					location.reload();
				};
			})
			.fail(function() {
			})
			.always(function() {
			});
		});
		$('#myModal').on('shown', function(event) {
			event.preventDefault();
			/* Act on the event */
		});

		$('#form_payment').submit(function(event) {
			/* Act on the event */
			event.preventDefault();

		});
	});
</script>